package com.example.myprojectoffeixue;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.apache.http.entity.StringEntity;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class ChoiceSbActivity extends Activity {
    TextView set_finish;
    ArrayList<SheBeiClass> all;
    ArrayList<String> flag=new ArrayList<String>();
    MyApplication m;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_sb);
        info();
    }
public void info(){
    Intent it=getIntent();
    final boolean flag2=it.getBooleanExtra("update",false);
    m= (MyApplication) getApplication();
    ListView all_device = (ListView) findViewById(R.id.all_device);
    if (flag == null) {
        flag = new ArrayList<String>();
    } else {
        flag.clear();
    }
    if (m.Nodevice_house.devices.size() != 0) {
        for (int i = 0; i < m.Nodevice_house.devices.size(); i++) {
            flag.add("0");
        }
    }
    adapter_Choicesb  kt = new adapter_Choicesb(ChoiceSbActivity.this, flag);
    all_device.setAdapter(kt);
    LinearLayout tfinish = (LinearLayout) findViewById(R.id.iv_mykaiguan);
    tfinish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }});
    set_finish = (TextView) findViewById(R.id.wancheng);
    set_finish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!flag2){
            addroom();}else{
                updateroom();
            }
        }
    });
}
    public void addroom() {
        HouseUtil h = new HouseUtil();
        h.userId=m.userId;
        h.name=m.name_house;
        h.deviceList=new ArrayList<SheBeiClass>();
        for(int i=0;i<m.Nodevice_house.devices.size();i++){
            if("1".equals(flag.get(i))){
                h.deviceList.add(m.Nodevice_house.devices.get(i));
            }
        }
        String j = JSON.toJSONString(h);
        Log.d("cmd",j);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.addLabel, params,

                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        ToastUtils.showToast(getBaseContext(),"网络异常");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("result5555",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {
                                Intent it=new Intent(getBaseContext(),MainActivity.class);
                                startActivity(it);
                                finish();
                            }else{
                                HttpConnection.toastLog("服务器错误", getBaseContext());
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", getBaseContext());
                        }
                    }

                });
    }


public void updateroom(){
    HouseUtil h = new HouseUtil();
    h.userId=m.userId;
    h.name=m.hu.name;
    h.id=m.hu.id;
    h.deviceList=new ArrayList<SheBeiClass>();
    h.deviceList.addAll(m.hu.devices);
    for(int i=0;i<m.Nodevice_house.devices.size();i++){
        if("1".equals(flag.get(i))){
            h.deviceList.add(m.Nodevice_house.devices.get(i));
        }
    }
    String j = JSON.toJSONString(h);
    Log.d("cmd444",j);
    j = AESUtil.encrypt(j);
    HttpUtils httpUtils = new HttpUtils();
    RequestParams params = new RequestParams();
    params.setHeader("Content-Type", "application/json");
    try {
        params.setBodyEntity(new StringEntity(j, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.updateLabel, params,

            new RequestCallBack<String>() {

                @Override
                public void onFailure(HttpException arg0, String arg1) {
                    ToastUtils.showToast(getBaseContext(),"网络异常");
                }

                @Override
                public void onSuccess(ResponseInfo<String> arg0) {
                    if (arg0.result != null && !("".equals(arg0.result))) {
                        arg0.result = AESUtil.decrypt(arg0.result);
                        Log.d("result5555",arg0.result);
                        JSONObject jb = JSONObject.parseObject(arg0.result);
                        String msg = jb.getString("msg");
                        if ("成功".equals(msg)) {
                            for(int i=0;i<m.Nodevice_house.devices.size();i++){
                                if("1".equals(flag.get(i))){
                                    m.hu.devices.add(m.Nodevice_house.devices.get(i));
                                }
                            }
                            finish();
                        }else{
                            HttpConnection.toastLog("服务器错误", getBaseContext());
                        }
                    } else {
                        HttpConnection.toastLog("服务器错误", getBaseContext());
                    }
                }

            });

















}



























}
